package com.wzy.web.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.adobe.agl.text.SimpleDateFormat;
import com.adobe.internal.pdftoolkit.services.javascript.stubs.auto.treeItem;
import com.wzy.web.pojo.PmActor;
import com.wzy.web.pojo.PmAffix;
import com.wzy.web.pojo.PmArticle;
import com.wzy.web.pojo.PmLeader;
import com.wzy.web.pojo.PmProcess;
import com.wzy.web.pojo.PmProject;
import com.wzy.web.pojo.PmUser;
import com.wzy.web.vo.AffixVO;
import com.wzy.web.vo.ArticleVO;
import com.wzy.web.vo.ProcessVO;
import com.wzy.web.vo.ProjectVO;

public class ProjectDataService extends BasicService {
	
	
	/**
	 * 
	 * @descr 得到系统时间
	 * @author wangzy 
	 * @date 2009-12-7 
	 * @return
	 */
	public String getSystemTime() {
		String returnTime = "";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm EEEE");
		returnTime = df.format(new Date());
		return returnTime;
	}
	/**
	 * 
	 * @descr 加载所有的项目 
	 * @author wangzy 
	 * @date 2009-12-29 
	 * @return
	 */
	public Map<String, Object> findAllProject() {
		Map<String, Object> reMap = new HashMap<String, Object>();
		List<PmProject> list = daoPmProject.findAll();
		List<ProjectVO> tempList = new ArrayList<ProjectVO>();
		ProjectVO vo = null;
		if(list != null) {
			for(int i = 0;i < list.size();i++) {
				vo = new ProjectVO();
				vo.setProjectId(list.get(i).getProjectId().toString());
				vo.setProjectName(list.get(i).getProjectName());
				vo.setProjectDetail(list.get(i).getProjectDetail());
				Iterator<PmLeader> leaderSet = list.get(i).getPmLeaders().iterator();
				String tempStr = "";
				while(leaderSet.hasNext()) {
					PmLeader leaderVo = leaderSet.next();
					tempStr += leaderVo.getPmUser().getUserName()+",";
				}
				vo.setLeaders(tempStr.substring(0, tempStr.length()-1));
				vo.setDateTime(list.get(i).getDateTime());
				tempList.add(vo);
			}
			reMap.put("VALUE", tempList);
			reMap.put("SORF", "true");
		} else {
			reMap.put("SORF", "false");
			reMap.put("VALUE", null);
		}
		return reMap;
	}
	/**
	 * 
	 * @descr 保存项目
	 * @author wangzy 
	 * @date 2009-12-29 
	 * @param jsonObj
	 * @return
	 */
	public Map<String, Object> saveProject(Object jsonObj) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		String sysDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
		JSONObject js = JSONObject.fromObject(jsonObj);
		String projectName = js.getString("projectName");
		JSONArray jsonArr = JSONArray.fromObject(js.getString("arLeaders"));
		PmProject vo = new PmProject();
		if(!js.getString("projectId").equals("-1")) {
			vo.setProjectId(Integer.parseInt(js.getString("projectId")));
		}
		vo.setDateTime(sysDate);
		vo.setProjectName(projectName);
		PmProject reVO = daoPmProject.merge(vo);
		Set<PmLeader> pmLeaders = new HashSet<PmLeader>(0);
		for(int i = 0;i < jsonArr.size();i++) {
			PmLeader leaderVO = new PmLeader();
			leaderVO.setPmProject(reVO);
			leaderVO.setPmUser(daoPmUser.findById(Integer.parseInt(jsonArr.get(i).toString())));
			pmLeaders.add(leaderVO);
		}
		reVO.setPmLeaders(pmLeaders);
		ProjectVO projectVo = null;
		PmProject tempVO = null;
		try {
			tempVO = daoPmProject.merge(reVO);
			if(tempVO != null) {
				projectVo = new ProjectVO();
				Iterator<PmLeader> leaderSet = tempVO.getPmLeaders().iterator();
				String tempStr = "";
				while(leaderSet.hasNext()) {
					PmLeader leaderVo = leaderSet.next();
					tempStr += leaderVo.getPmUser().getUserName()+",";
				}
				projectVo.setProjectId(""+tempVO.getProjectId());
				projectVo.setProjectName(tempVO.getProjectName());
				projectVo.setLeaders(tempStr.substring(0, tempStr.length()-1));
				projectVo.setDateTime(tempVO.getDateTime());
				reMap.put("VALUE", projectVo);
				reMap.put("SORF", "true");
			} else {
				reMap.put("SORF", "false");
				reMap.put("VALUE", null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			reMap.put("SORF", "false");
			reMap.put("VALUE", null);
		}
		return reMap;
	}
	/**
	 * 
	 * @descr 加载指定ID项目
	 * @author wangzy 
	 * @date 2009-12-29 
	 * @param projectId
	 * @return
	 */
	public Map<String, Object> findProjectById(String projectId) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		List list = new ArrayList();
 		PmProject vo = null;
		ProjectVO tempVo = new ProjectVO();
		try {
			vo = daoPmProject.findById(Integer.parseInt(projectId));
			Iterator<PmLeader> leaderSet = vo.getPmLeaders().iterator();
			String tempStr = "";
			while(leaderSet.hasNext()) {
				PmLeader leaderVo = leaderSet.next();
				list.add(""+leaderVo.getPmUser().getUserId());
			}
			tempVo.setLeaderList(list);
			tempVo.setProjectName(vo.getProjectName());
			reMap.put("VALUE", tempVo);
			reMap.put("SORF", "true");
		} catch (Exception e) {
			e.printStackTrace();
			reMap.put("SORF", "false");
			reMap.put("VALUE", null);
		}
		return reMap;
	}
	/**
	 * 
	 * @descr 删除项目
	 * @author wangzy 
	 * @date 2009-12-29 
	 * @param projcetId
	 * @return
	 */
	public Map<String, Object> deleteProjectById(String projcetId) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		try {
			daoPmProject.delete(daoPmProject.findById(Integer.parseInt(projcetId)));
			reMap.put("SORF", "true");
		} catch (Exception e) {
			e.printStackTrace();
			reMap.put("SORF", "false");
		}
		return reMap;
	}
	/**
	 * 
	 * @descr 保存/修改项目详细信息
	 * @author wangzy 
	 * @date 2009-12-29 
	 * @param projectDetail
	 * @param projectId
	 * @return
	 */
	public Map<String, Object> saveProjectDetail(String projectDetail,String projectId) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		try {
			PmProject vo = daoPmProject.findById(Integer.parseInt(projectId));
			vo.setProjectDetail(projectDetail);
			daoPmProject.merge(vo);
			reMap.put("SORF", "true");
		} catch (Exception e) {
			e.printStackTrace();
			reMap.put("SORF", "false");
		}
		return reMap;
	}
	/**
	 * 
	 * @descr 加载所有项目的详细信息 
	 * @author wangzy 
	 * @date 2009-12-29 
	 * @return
	 */
	public Map<String, Object> findAllDetailProject() {
		Map<String, Object> reMap = new HashMap<String, Object>();
		List<ProjectVO> resultList = new ArrayList<ProjectVO>();
		List<PmProject> projectList = null;
		PmProject projectVo = null;
		ProjectVO vo = null;
		try {
			projectList = daoPmProject.findAll();
			if(projectList != null) {
				for(int i = 0;i < projectList.size();i++) {
					projectVo = projectList.get(i);
					List leaderList = new ArrayList();
					List affixList = new ArrayList();
					List aritcleList = new ArrayList();
					List processList = new ArrayList();
					Set<PmLeader> leaderSet = projectVo.getPmLeaders();
					Set<PmAffix> affixSet = projectVo.getPmAffix();
					Set<PmArticle> articleSet = projectVo.getPmArticle();
					List<PmProcess> processSet = daoPmProcess.findByProperty("pmProject", projectVo);
					vo = new ProjectVO();
					vo.setProjectId(projectVo.getProjectId().toString());
					vo.setProjectName(projectVo.getProjectName());
					vo.setProjectDetail(projectVo.getProjectDetail());
					if(leaderSet != null) {
						Iterator<PmLeader> leaderIT = leaderSet.iterator();
						while(leaderIT.hasNext()) {
							PmLeader leaderVo = leaderIT.next();
							leaderList.add(leaderVo.getPmUser());
						}
					}
					vo.setLeaderList(leaderList);
					if(affixSet != null) {
						Iterator<PmAffix> affixIT = affixSet.iterator();
						while(affixIT.hasNext()) {
							PmAffix affixVo = affixIT.next();
							AffixVO reVo = new AffixVO();
							if(affixVo != null) {
								reVo.setAffixId(affixVo.getAffixId().toString());
								reVo.setAffixName(affixVo.getAffixName());
								reVo.setAffixDetail(affixVo.getAffixDetail());
								reVo.setAffixUrl(affixVo.getAffixUrl());
								reVo.setDateTime(affixVo.getDateTime());
								reVo.setUserName(affixVo.getPmUser().getUserName());
							}
							affixList.add(reVo);
						}
					}
					vo.setAffixList(affixList);
					if(aritcleList != null) {
						Iterator<PmArticle> articleIT = articleSet.iterator();
						ArticleVO reVo = null;
						while (articleIT.hasNext()) {
							PmArticle articleVo = articleIT.next();
							reVo = new ArticleVO();
							reVo.setArticleContent(articleVo.getArticleContent());
							reVo.setArticleId(articleVo.getArticleId().toString());
							reVo.setArticleName(articleVo.getArticleName());
							reVo.setDateTime(articleVo.getDateTime());
							reVo.setUserName(articleVo.getPmUser().getUserName());
							reVo.setProjectId(articleVo.getPmProject().getProjectId().toString());
							reVo.setUserId(articleVo.getPmUser().getUserId().toString());
							aritcleList.add(reVo);
						}
					}
					vo.setAritcleList(aritcleList);
					if(processSet != null) {
						//Iterator<PmProcess> processIT = processSet.iterator();
						//while (processIT.hasNext()) {
							for(int n = 0;n < processSet.size();n++) {
							PmProcess processVo = processSet.get(n);
							ProcessVO tempVo = new ProcessVO();
							tempVo.setProcessId(processVo.getProcessId().toString());
							tempVo.setProcessName(processVo.getProcessName());
							tempVo.setProcessStartDate(processVo.getProcessStartDate());
							tempVo.setProcessEndDate(processVo.getProcessEndDate());
							tempVo.setProcessDetail(processVo.getProcessDetail());
							Set<PmActor> actorSet = processVo.getPmActors();
							List<PmUser> actorList = new ArrayList<PmUser>();
							if(actorSet != null) {
								Iterator<PmActor> actorIT = actorSet.iterator();
								while(actorIT.hasNext()) {
									actorList.add(actorIT.next().getPmUser());
								}
							}
							tempVo.setActorList(actorList);
							processList.add(tempVo);
						}
					}
					vo.setProcessList(processList);
					resultList.add(vo);
				}
				reMap.put("VALUE", resultList);
				reMap.put("SORF", "true");
			}
		} catch (Exception e) {
			e.printStackTrace();
			reMap.put("VALUE", null);
			reMap.put("SORF", "false");
		}
		return reMap;
	}
	/**
	 * 
	 * @descr 保存项目进度
	 * @author wangzy 
	 * @date 2009-12-2 
	 * @param jsonObj
	 * @return
	 */
	public Map<String, Object> saveProcess(Object jsonObj) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		PmProcess processVo = new PmProcess();
		JSONObject js = JSONObject.fromObject(jsonObj);
		try {
			processVo.setProcessName(js.getString("processName"));
			processVo.setProcessStartDate(js.getString("startTime"));
			processVo.setProcessEndDate(js.getString("endTime"));
			processVo.setProcessDetail(js.getString("processContent"));
			processVo.setDateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			processVo.setPmProject(daoPmProject.findById(Integer.parseInt(js.getString("projectId"))));
			PmProcess tempVo = daoPmProcess.merge(processVo);
			
			Set<PmActor> actorSet = new HashSet<PmActor>();
			PmActor actorVo = null;
			JSONArray jsArr = JSONArray.fromObject(js.getString("actors"));
			for(int i = 0;i < jsArr.size();i++) {
				actorVo = new PmActor();
				PmUser userVo = new PmUser();
				userVo.setUserId(Integer.parseInt(jsArr.get(i).toString()));
				actorVo.setPmUser(userVo);
				actorVo.setPmProcess(tempVo);
				actorSet.add(actorVo);
			}
			tempVo.setPmActors(actorSet);
			tempVo = daoPmProcess.merge(tempVo);
			if(tempVo != null) {
				reMap.put("VALUE", tempVo);
				reMap.put("SORF", "true");
			} else {
				reMap.put("VALUE", null);
				reMap.put("SORF", "false");
			}
		} catch (Exception e) {
			reMap.put("VALUE", null);
			reMap.put("SORF", "false");
			e.printStackTrace();
		}
		
		return reMap;
	}
	/**
	 * 
	 * @descr 修改进度
	 * @author wangzy 
	 * @date 2009-12-10 
	 * @param jsonObj
	 * @return
	 */
	public Map<String, Object> updateProcess(Object jsonObj) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		PmProcess processVo = new PmProcess();
		JSONObject js = JSONObject.fromObject(jsonObj);
		try {
			daoPmProcess.deleteDefine(Integer.parseInt(js.getString("processId")));
			processVo.setProcessId(Integer.parseInt(js.getString("processId")));
			processVo.setProcessName(js.getString("processName"));
			processVo.setProcessStartDate(js.getString("startTime"));
			processVo.setProcessEndDate(js.getString("endTime"));
			processVo.setProcessDetail(js.getString("processContent"));
			processVo.setDateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			processVo.setPmProject(daoPmProject.findById(Integer.parseInt(js.getString("projectId"))));
			
			Set<PmActor> actorSet = new HashSet<PmActor>();
			PmActor actorVo = null;
			JSONArray jsArr = JSONArray.fromObject(js.getString("actors"));
			for(int i = 0;i < jsArr.size();i++) {
				actorVo = new PmActor();
				PmUser userVo = new PmUser();
				userVo.setUserId(Integer.parseInt(jsArr.get(i).toString()));
				actorVo.setPmUser(userVo);
				actorVo.setPmProcess(processVo);
				actorSet.add(actorVo);
			}
			processVo.setPmActors(actorSet);
			PmProcess tempVo = daoPmProcess.merge(processVo);
			if(tempVo != null) {
				reMap.put("VALUE", tempVo);
				reMap.put("SORF", "true");
			} else {
				reMap.put("VALUE", null);
				reMap.put("SORF", "false");
			}
		} catch (Exception e) {
			reMap.put("VALUE", null);
			reMap.put("SORF", "false");
			e.printStackTrace();
		}
		
		return reMap;
	}
	/**
	 * 
	 * @descr 删除进度
	 * @author wangzy 
	 * @date 2009-12-10 
	 * @param id
	 * @return
	 */
	public Map<String, Object> deleteProcess(String id) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		try {
			daoPmProcess.deleteDefine(Integer.parseInt(id));
			daoPmProcess.delete(daoPmProcess.findById(Integer.parseInt(id)));
			reMap.put("SORF", "true");
		} catch (Exception e) {
			reMap.put("VALUE", null);
			reMap.put("SORF", "false");
			e.printStackTrace();
		}
		return reMap;
	}
	/**
	 * 
	 * @descr 加载项目对应所有的附件 
	 * @author wangzy 
	 * @date 2009-12-3 
	 * @param projectId
	 * @return
	 */
	public Map<String, Object> findAllAffix(String projectId) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		List<AffixVO> reList = new ArrayList<AffixVO>();
		PmProject project = new PmProject();
		project.setProjectId(Integer.parseInt(projectId));
		try {
			List<PmAffix> affixList = daoPmAffix.findByProperty("pmProject", project);
			if(affixList != null) {
				AffixVO vo = null;
				for(PmAffix item : affixList) {
					vo = new AffixVO();
					vo.setAffixId(item.getAffixId().toString());
					vo.setAffixName(item.getAffixName());
					vo.setAffixDetail(item.getAffixDetail());
					vo.setAffixUrl(item.getAffixUrl());
					vo.setDateTime(item.getDateTime());
					vo.setUserName(item.getPmUser().getUserName());
					
					reList.add(vo);
				}
			}
			reMap.put("VALUE", reList);
			reMap.put("SORF", "true");
		} catch (Exception e) {
			reMap.put("VALUE", null);
			reMap.put("SORF", "false");
			e.printStackTrace();
		}
		
		return reMap;
	}
	/**
	 * 
	 * @descr 保存附件
	 * @author wangzy 
	 * @date 2009-12-3 
	 * @param jsonObj
	 * @return
	 */
	public Map<String, Object> saveAffix(Object jsonObj) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		JSONObject js = JSONObject.fromObject(jsonObj);
		PmAffix vo = new PmAffix();
		try {
			vo.setAffixName(js.getString("affixName"));
			vo.setAffixUrl(js.getString("affixUrl"));
			vo.setAffixDetail(js.getString("affixDetail"));
			vo.setDateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			vo.setPmProject(daoPmProject.findById(Integer.parseInt(js.getString("projectId"))));
			vo.setPmUser(daoPmUser.findById(Integer.parseInt(js.getString("userId"))));
			PmAffix tempVo = daoPmAffix.merge(vo);
			AffixVO reVo = new AffixVO();
			if(tempVo != null) {
				reVo.setAffixId(tempVo.getAffixId().toString());
				reVo.setAffixName(tempVo.getAffixName());
				reVo.setAffixDetail(tempVo.getAffixDetail());
				reVo.setAffixUrl(tempVo.getAffixUrl());
				reVo.setDateTime(tempVo.getDateTime());
				reVo.setUserName(tempVo.getPmUser().getUserName());
			}
			reMap.put("VALUE", reVo);
			reMap.put("SORF", "true");
		} catch (Exception e) {
			reMap.put("VALUE", null);
			reMap.put("SORF", "false");
			e.printStackTrace();
		}
		return reMap;
	}
	/**
	 * 
	 * @descr 保存总结
	 * @author wangzy 
	 * @date 2009-12-6 
	 * @param jsonObj
	 * @return
	 */
	public Map<String, Object> saveArtile(Object jsonObj) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		JSONObject js = JSONObject.fromObject(jsonObj);
		PmArticle vo = new PmArticle();
		try {
			vo.setArticleName(js.getString("articleName"));
			vo.setArticleContent(js.getString("articleContent"));
			vo.setDateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			vo.setPmProject(daoPmProject.findById(Integer.parseInt(js.getString("projectId"))));
			vo.setPmUser(daoPmUser.findById(Integer.parseInt(js.getString("userName"))));
			PmArticle tempVo = daoPmArticle.merge(vo);
			ArticleVO reVo = new ArticleVO();
			if(tempVo != null) {
				reVo.setArticleContent(tempVo.getArticleContent());
				reVo.setArticleId(tempVo.getArticleId().toString());
				reVo.setArticleName(tempVo.getArticleName());
				reVo.setDateTime(tempVo.getDateTime());
				reVo.setUserName(tempVo.getPmUser().getUserName());
				reVo.setUserId(tempVo.getPmUser().getUserId().toString());
			}
			reMap.put("VALUE", reVo);
			reMap.put("SORF", "true");
		} catch (Exception e) {
			reMap.put("VALUE", null);
			reMap.put("SORF", "false");
			e.printStackTrace();
		}
		return reMap;
	}
	/**
	 * 
	 * @descr 修改总结
	 * @author wangzy 
	 * @date 2009-12-6 
	 * @param jsonObj
	 * @return
	 */
	public Map<String, Object> updateArtile(Object jsonObj) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		JSONObject js = JSONObject.fromObject(jsonObj);
		PmArticle vo = new PmArticle();
		try {
			vo.setArticleName(js.getString("articleName"));
			vo.setArticleContent(js.getString("articleContent"));
			vo.setDateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			vo.setPmProject(daoPmProject.findById(Integer.parseInt(js.getString("projectId"))));
			vo.setPmUser(daoPmUser.findById(Integer.parseInt(js.getString("userName"))));
			vo.setArticleId(Integer.parseInt(js.getString("articleId")));
			PmArticle tempVo = daoPmArticle.merge(vo);
			ArticleVO reVo = new ArticleVO();
			if(tempVo != null) {
				reVo.setArticleContent(tempVo.getArticleContent());
				reVo.setArticleId(tempVo.getArticleId().toString());
				reVo.setArticleName(tempVo.getArticleName());
				reVo.setDateTime(tempVo.getDateTime());
				reVo.setUserName(tempVo.getPmUser().getUserName());
				reVo.setUserId(tempVo.getPmUser().getUserId().toString());
			}
			reMap.put("VALUE", reVo);
			reMap.put("SORF", "true");
		} catch (Exception e) {
			reMap.put("VALUE", null);
			reMap.put("SORF", "false");
			e.printStackTrace();
		}
		return reMap;
	} 
	/**
	 * 
	 * @descr 删除文章
	 * @author wangzy 
	 * @date 2009-12-10 
	 * @param id
	 * @return
	 */
	public Map<String, Object> deleteArticle(String id) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		try {
			daoPmArticle.delete(daoPmArticle.findById(Integer.parseInt(id)));
			reMap.put("SORF", "true");
		} catch (Exception e) {
			reMap.put("VALUE", null);
			reMap.put("SORF", "false");
			e.printStackTrace();
		}
		return reMap;
	}
}
